package com.dmwork.service.work.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.service.work.entity.Work;
import com.dmwork.service.work.entity.WorkVideoInfo;
import com.dmwork.service.work.feign.VodVideoFeignService;
import com.dmwork.service.work.mapper.WorkMapper;
import com.dmwork.service.work.mapper.WorkVideoInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmwork.service.work.mq.sender.MessageSender;
import com.dmwork.service.work.service.WorkVideoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-03-14
 */
@Service
public class WorkVideoInfoServiceImpl extends ServiceImpl<WorkVideoInfoMapper, WorkVideoInfo> implements WorkVideoInfoService {

    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private MessageSender messageSender;

    @Override
    public List<String> getVideoSourceIdByWorkId(String workId) {
        QueryWrapper<WorkVideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId);
        List<WorkVideoInfo> workVideoInfoList = baseMapper.selectList(queryWrapper);
        List<String> videoSourceIdList = new ArrayList<>();
        for (WorkVideoInfo workVideoInfo : workVideoInfoList) {
            videoSourceIdList.add(workVideoInfo.getVideoSourceId());
        }
        return videoSourceIdList;
    }

    @Override
    public void removeByWorkId(String workId) {
        QueryWrapper<WorkVideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void removeVideoSourceByWorkId(String workId) {
        QueryWrapper<WorkVideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId);
        List<WorkVideoInfo> workVideoInfoList = baseMapper.selectList(queryWrapper);
        List<String> videoSourceIdList = new ArrayList<>();
        for (WorkVideoInfo workVideoInfo : workVideoInfoList) {
            videoSourceIdList.add(workVideoInfo.getVideoSourceId());
        }
        if (videoSourceIdList.size()>0) {
            messageSender.sendBatchVideoRemoveMessage(videoSourceIdList, new HashMap<>());
        }
    }

    @Override
    public void removeVideoInfoByIds(String workId, List<String> idList) {
        //更新视频数量
        Work work = workMapper.selectById(workId);
        work.setVideoCount(work.getVideoCount() - idList.size());
        workMapper.updateById(work);
        //批量删除视频信息
        baseMapper.deleteBatchIds(idList);
    }
}
